package com.wujilee.wujileejuc.future;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * @author liwuji
 * @date 2024/7/8
 * @desc
 **/
public class FutureTaskTest {
    private static volatile int count = 0;
    private static Map<String,FutureTask<String>> cache = new ConcurrentHashMap<>();

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        FutureTask<String> futureTask = new FutureTask<>(() -> {
            try {
                Thread.sleep(5000L);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            count = 1;
            System.out.println("子线程执行完");
            return count + "";
        });
        cache.put("f1", futureTask);
        executorService.execute(futureTask);


        Thread.sleep(100L);
        FutureTask<String> f1 = cache.get("f1");
        String string = f1.get();
        System.out.println("主线程执行");
        System.out.println("主线程 获取子线程执行结果"+ string);

        Thread.sleep(10000L);

    }
}
